<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="机构" prop="departmentId">
        <el-select
          v-model="queryParams.departmentId"
          placeholder="请选择机构"
          filterable
          clearable
          @change="deptChange"
        >
          <el-option
            v-for="item in departmentList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="停车场" prop="parkId">
          <el-select v-model="queryParams.parkId" placeholder="请选择" clearable >
            <el-option
              v-for="item in allParkList"
              :key="item.id"
              :label="item.name"
              :value="item.id"/>
          </el-select>
        </el-form-item>
      <el-form-item label="泊位编码" prop="berthCode">
        <el-input
          v-model="queryParams.berthCode"
          placeholder="请输入泊位编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="泊位异常类型" prop="berthExceptionType">
        <el-select v-model="queryParams.berthExceptionType" placeholder="请选择泊位异常类型" clearable>
          <el-option
            v-for="item in dict.type.EBerthExceptionType"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="时间" prop="dateRange">
        <el-date-picker
          v-model="queryParams.dateRange"
          type="datetimerange"
          range-separator="-"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          :default-time="['00:00:00','23:59:59']">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="spparkplaceList" >
      <el-table-column 
      v-for="(item,index) in tableList"
      :key="index"
      :render-header="item.prop == 'count' ? renderHeader : ''"
      :label="item.label" align="center" :prop="item.prop" :width="item.width ? item.width : ''">
        <template 
        slot-scope="{row}">
          <dict-tag v-if="item.prop == 'berthStatus'" :options="dict.type.EBerthStatus" :value="row[item.prop]"/>
          <dict-tag v-else-if="item.prop == 'type'" :options="dict.type.EBerthExceptionType" :value="row[item.prop]"/>
          <div v-else-if="item.prop == 'img'">
            <el-image
              v-if="row[item.prop]"
              style="width: 100px; height: 100px"
              :preview-src-list="[row[item.prop]]"
              :src="row[item.prop]"></el-image>
          </div>
          <div v-else>{{row[item.prop]}}</div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {fetchList,} from "@/api/lcPark/abnormal";
import { getDepartmentList,getListAuthPark } from '@/api/base'
import { timesToYyMmDd1,YyMmDdToTimes,getEndStartTime } from "@/utils/time.js";
export default {
  name: "abnormal",
  dicts: ['EBerthStatus','EBerthExceptionType'],
  components:{},
  data() {
    return {
      //提示语
      renderHeader(h, {column}) {
        return h('div', [
          h('span', column.label),
          h('el-tooltip', {
              undefined,
              props: {
                undefined,
                effect: 'dark',
                content: "累计次数仅记录该泊位异常类型为有车显无车、无车显有车的数据",
                placement: 'top',
              },
            },
            [h('i', {
              undefined,
              class: 'el-icon-question',
              style: "color:#409eff;margin-left:5px;cursor:pointer; font-size:16px;"
            })],
            {content: "累计次数仅记录该泊位异常类型为有车显无车、无车显有车的数据"})

        ]);
      },
      tableList:[
        {label:'机构名称',prop:'departmentName',},
        {label:'片区名称',prop:'parkLotName',},
        {label:'泊位编码',prop:'berthCode',},
        {label:'泊位状态',prop:'berthStatus',},
        {label:'累计次数',prop:'count',},
        {label:'异常类型',prop:'type',},
        {label:'图片',prop:'img',},
        {label:'上报人员姓名',prop:'createBy',},
        {label:'上报时间',prop:'createTime',},
      ],//表格字段显示
      //片区数据
      areaList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车位信息表格数据
      spparkplaceList: [],
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
        dateRange:[],
      },
      departmentList: [],//机构数据
      allParkList: [],//停车场数据
    };
  },
  created() {
    this.getDepartmentList();
    this.getTimeData();
  },
  methods: {
    getTimeData() {
      let starTimes = timesToYyMmDd1(new Date(),'YY-MM-DD')
      let end = YyMmDdToTimes(timesToYyMmDd1(new Date())) + 24 * 60 * 60 * 1000
      let endTime1 = timesToYyMmDd1(end,'YY-MM-DD')
      let getEndStartTimes = getEndStartTime([starTimes,starTimes])
      let getEndStartTimes1 = getEndStartTime([endTime1,endTime1])
      let starTime = getEndStartTimes.startTime
      let endTime = getEndStartTimes1.startTime
      console.log([
        starTime,
        endTime,
      ])
      this.queryParams.dateRange = [
        starTime,
        endTime,
      ]
      this.getList();
      // timesData(query).then(response => {
      //   if (response.code != 200) return;
      //   this.queryParams.dateRange = response.data;
      //   this.getList();
      // });
    },
    async deptChange(e){//机构下拉选择
      let queryParams = {...this.queryParams}
      queryParams.parkId = ''
      this.queryParams = {...queryParams}
      if(!e) return this.allParkList = []
      this.allParkList = await this.getListAuthPark(e)
    },
    getDepartmentList(){//获取机构
      getDepartmentList().then(res => {
        this.departmentList = res.data || []
      })
    },
    async getListAuthPark(departmentId){//获取停车场
      let res = await getListAuthPark({departmentId,parkType:2})
      return res.data || []
    },
    /** 查询车位信息列表 */
    getList() {

      this.loading = true;
      if (!this.queryParams.dateRange) this.queryParams.dateRange = [];
      let queryParams = {
        ...this.queryParams
      }
      if(queryParams.dateRange.length) {
        queryParams.startTime = queryParams.dateRange[0]
        queryParams.endTime = queryParams.dateRange[1]
      }
      delete queryParams.dateRange
      fetchList(queryParams).then(response => {
        let data = response.data || {}
        let rows = data.list || []
        this.spparkplaceList = rows
        this.total = data.total || 0;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.dateRange = []
      this.handleQuery();
    },
  }
};
</script>
<style lang="scss">
 .amap-icon img,.amap-icon,
.amap-marker-content img{
    width: 25px!important;
    height: 34px!important;
}
#container {width:650px; height: 300px; }
</style>
